<template>
  <div id="replace">
    <Bread />
    <!-- 查询栏 -->
    <el-row>
      <el-form
        :inline="true"
        :model="formInline"
        ref="formRef"
        class="demo-form-inline addForm"
      >
        <el-form-item>
          <el-input
            v-model="formInline.cardNum"
            placeholder="会员卡号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.name" placeholder="会员名字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.payType" placeholder="支付类型">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in opation"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="formInline.birthday"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="inquire">查询</el-button>
          <el-button type="primary" @click="add">新增</el-button>
          <el-button type="primary" @click="rest">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="tableData" border>
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="cardNum" label="会员卡号"> </el-table-column>
      <el-table-column prop="name" label="会员姓名"> </el-table-column>
      <el-table-column prop="birthday" label="会员生日"> </el-table-column>
      <el-table-column prop="phone" label="手机号码"> </el-table-column>
      <el-table-column prop="integral" label="可用积分"> </el-table-column>
      <el-table-column prop="money" label="开卡金额"> </el-table-column>
      <el-table-column prop="payType" label="支付类型">
        <template v-slot="scope">
          <span v-if="scope.row.payType == 1">微信</span>
          <span v-if="scope.row.payType == 2">银行卡</span>
          <span v-if="scope.row.payType == 3">支付宝</span>
          <span v-if="scope.row.payType == 4">现金</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址"> </el-table-column>
      <el-table-column label="操作">
        <!-- 操作区域 -->
        <template v-slot="scope">
          <el-button
            size="mini"
            class="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            class="el-icon-edit"
            @click="edit(scope)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 6, 12, 18]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增数据对话框 -->
    <el-dialog
      :title="titleText + '用户'"
      :visible.sync="addDialogVisible"
      width="30%"
    >
      <!-- 新增form表单 -->
      <el-form
        :model="addForm"
        :rules="addRules"
        label-width="80px"
        ref="addRef"
      >
        <el-form-item label="会员卡号" prop="cardNum">
          <el-input v-model="addForm.cardNum"></el-input>
        </el-form-item>
        <el-form-item label="会员姓名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="会员生日">
          <el-date-picker
            type="date"
            placeholder="会员生日"
            v-model="addForm.birthday"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="开卡金额">
          <el-input v-model="addForm.money"></el-input>
        </el-form-item>
        <el-form-item label="可用积分">
          <el-input v-model="addForm.integral"></el-input>
        </el-form-item>
        <el-form-item prop="payType" label="支付类型">
          <el-select v-model="addForm.payType" placeholder="支付类型">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in opation"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员地址">
          <el-input type="textarea" v-model="addForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Bread from "./bread";
import userApi from "../api/Users";
export default {
  components: {
    Bread,
  },
  data() {
    return {
      addDialogVisible: false, //添加对话框初始状态
      addForm: {
        id: null,
        cardNum: "",
        name: "",
        birthday: "",
        phone: "",
        money: 0,
        integral: 0,
        payType: "",
        address: "",
      },
      formInline: {},
      flag: false,
      // 多选数据
      opation: [
        { value: 1, label: "微信" },
        { value: 2, label: "现金" },
        { value: 3, label: "支付宝" },
        { value: 4, label: "银行卡" },
      ],
      queryInfo: {
        query: "",
        pagesize: 5,
        pagenum: 10,
      },
      tableData: [],
      total: 0,
      // 校验规则
      addRules: {
        cardNum: [
          { required: true, message: "请输入会员卡号", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入会员姓名", trigger: "blur" }],
        payType: [
          { required: true, message: "请选择付款方式", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    // console.log(this.titleText);
  },
  methods: {
    inquire() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return;
        this.getList();
      });
    },
    // 新增
    add() {
      this.flag = true;
      this.addDialogVisible = true;
      this.addForm.id = "";
      this.addForm.cardNum = "";
      this.addForm.name = "";
      this.addForm.birthday = "";
      this.addForm.phone = "";
      this.addForm.money = 0;
      this.addForm.integral = 0;
      this.addForm.payType = "";
      this.addForm.address = "";
    },
    async addDialog() {
      if (this.flag) {
        console.log(this.addForm);
        const { data: res } = await userApi.add(this.addForm);
        if (res.code !== 2000) {
          this.$message.error("添加用户失败");
        }
        this.$message.success("添加用户成功");
      } else {
        const { data: res } = await userApi.edit(this.addForm.id, this.addForm);
        if (res.code !== 2000) {
          this.$message.error(res.message);
        } else {
          this.$message.success(res.message);
        }
      }
      this.addDialogVisible = false;
      this.getList();
    },

    // 获取所有数据
    async getList() {
      const { data: res } = await userApi.list(
        this.queryInfo.pagenum,
        this.queryInfo.pagesize
      );
      console.log(res);
      this.tableData = res.data.rows;
      this.total = this.tableData.length;
    },
    // 点击进行删除
    async del(id) {
      const confirmText = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmText !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await userApi.del(id);
      if (res.code !== 2000) {
        this.$message.error("删除会员失败");
      } else {
        this.$message.success("删除成功");
        this.getList();
      }
    },
    rest() {
      this.formInline = {};
    },
    async edit(item) {
      this.flag = false;
      if (!this.flag) {
        this.addForm = item.row;
        this.addDialogVisible = true;
      }
    },

    // 分页
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getList();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getList();
      console.log(`当前页: ${val}`);
    },
  },
  computed: {
    titleText() {
      return this.flag ? "添加" : "编辑";
    },
  },
};
</script>


<style lang='scss' scoped>
.el-card {
  margin-bottom: 15px;
}
.addForm {
  width: 100%;
  display: flex;
  align-items: center;
}
.el-card {
  display: flex;
}
.el-card span {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-left: 10px;
  box-sizing: border-box;
  border-left: 3px solid green;
}
</style>